package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.Good;
import com.design.softwaredesign.entity.User;
import com.design.softwaredesign.front.UserInfo;
import com.design.softwaredesign.mapper.BackCampusMapper;
import com.design.softwaredesign.mapper.BackUserMapper;
import com.design.softwaredesign.service.BackUserService;
import com.design.softwaredesign.vo.DataVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/4 16:08
 */
@Service
public class BackUserServiceImpl implements BackUserService {
    @Autowired
    private BackUserMapper backUserMapper;

    @Autowired
    private BackCampusMapper backCampusMapper;

    @Override
    public DataVo getUserInfo(String query, Integer pageNum, Integer pageSize, Integer userState) {
        DataVo<User> dataVo = new DataVo<>();
        try {
            PageHelper.startPage(pageNum, pageSize);
            if(query != null) {
                query = "%" + query + "%";
            }
            else {
                query = "%%";
            }
            List<User> userList = backUserMapper.getUserInfo(query, userState);
            PageInfo<User> pageInfo = new PageInfo<>(userList);
            dataVo.setCode(200);
            dataVo.setMsg("获取用户列表成功");
            dataVo.setCount(pageInfo.getTotal());
            dataVo.setData(userList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("获取用户列表失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo addUser(UserInfo userInfo) {
        DataVo dataVo = new DataVo();
        User user = new User();
        dataVo.setCode(202);
        dataVo.setMsg("添加用户失败");
        BeanUtils.copyProperties(userInfo, user);
        user.setUserId(UUID.randomUUID().toString());
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化当前系统日期
        String registerTime = dateFm.format(new java.util.Date());
        user.setUserRegisterTime(registerTime);


        if(backUserMapper.addUser(user) == 1) {
            dataVo.setCode(200);
            dataVo.setMsg("添加用户成功");
        }
        return null;
    }

    @Override
    public DataVo updateUserInfo(User user) {
        DataVo dataVo = new DataVo();
        dataVo.setMsg("用户信息修改失败");
        dataVo.setCode(202);
        try {

            if( backUserMapper.updateUserInfo(user) == 1) {
                dataVo.setMsg("用户信息修改成功");
                dataVo.setCode(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }

    @Override
    public DataVo deleteUser(String userId) {
        DataVo dataVo = new DataVo();
        dataVo.setMsg("删除用户失败");
        dataVo.setCode(202);
        try {
            List<Good> goodList = backCampusMapper.selectGoodByUserId(userId);
            for(int i = 0; i < goodList.size(); i++) {
                backCampusMapper.deleteGoodImagesByGoodId(goodList.get(i).getGoodId());
                backCampusMapper.deleteGoodDataByGoodId(goodList.get(i).getGoodId());
                backCampusMapper.deleteGoodByGoodId(goodList.get(i).getGoodId());
            }
            backUserMapper.deleteUserByUserId(userId);
            dataVo.setCode(200);
            dataVo.setMsg("删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }
}
